package com.onpositive.dataminer.rcp.views;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.jface.viewers.IStructuredSelection;

import com.onpositive.commons.elements.AbstractUIElement;
import com.onpositive.dataminer.core.AuditManager;
import com.onpositive.dataminer.core.AuditManager.ObjEntry;
import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.knowledge.model.values.IValueTransformer;
import com.onpositive.knowledge.model.values.PropertyValuePair;
import com.onpositive.semantic.model.api.changes.IValueListener;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.api.realm.Realm;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.ui.property.editors.structured.ListEnumeratedValueSelector;
import com.onpositive.semantic.ui.workbench.elements.SelectionBinding;
import com.onpositive.semantic.ui.workbench.elements.XMLView;

public class ParserInspectionView extends XMLView {

	private SelectionBinding m;

	public ParserInspectionView() {
		super("/ui/parserInspectionView.dlf");
		setSelectionProvider("valTab");
	}
	
	public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
		
		
		super.createPartControl(parent);
		getBinding().getBinding("sel").addValueListener(new IValueListener<Object>() {

			@Override
			public void valueChanged(Object oldValue, Object newValue) {
				if(newValue == null )
					return ;
				
				if ( newValue instanceof IProperty ) {
					Binding bnd = ParserInspectionView.this.getBinding("bnd");
					bnd.refresh() ;
					return ;
				}
			}
		});
	};

	public IRealm<IProperty> getProperties() {
		
		ArrayList<IProperty> result = new ArrayList<IProperty>() ;
		Iterable<IProperty> iterable = DefaultUniverse.getInstance().getProperties(null) ;		
		for( IProperty prop : iterable )
			result.add(prop) ;
		return new Realm<IProperty>( result ) ;
	}
	
	public Collection<? extends Object> getInspectionDelta()
	{
		final AbstractUIElement<?> element = getElement("props");
		if( element == null )
			return null ;
		
		IStructuredSelection sel = ((ListEnumeratedValueSelector<?>)element).getSelection() ;
		if( sel == null || sel.size() == 0 )
			return null;
		
		Object obj = sel.getFirstElement() ;
		if( !(obj instanceof IProperty) )
			return null;
		
		IProperty prop = (IProperty) obj;
		
		Collection<? extends Object> result = DefaultUniverse.getInstance().getDelta(prop) ;		
		
		return result ;
	}

}
